package com.entfrm.biz.it.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.entfrm.biz.it.entity.PropertyCategory;
import com.entfrm.biz.it.excel.PropertyExcel;
import com.entfrm.biz.it.excel.PropertyExcelVo;
import com.entfrm.biz.it.mapper.PropertyCategoryMapper;
import com.entfrm.biz.system.service.ConfigService;
import com.entfrm.core.base.exception.BaseException;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.entfrm.biz.it.mapper.PropertyMapper;
import com.entfrm.biz.it.entity.Property;
import com.entfrm.biz.it.service.PropertyService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author kyc
 * @date 2020-08-07 17:56:35
 * @description 资产信息Service业务层
 */
@Service
public class PropertyServiceImpl extends ServiceImpl<PropertyMapper, Property> implements PropertyService {

    @Autowired
    private PropertyMapper propertyMapper;

    @Autowired
    private PropertyCategoryMapper propertyCategoryMapper;

    @Override
    public List<PropertyExcel> listPropertyExcel(Property property) {
        return propertyMapper.listPropertyExcel(property);
    }


    @Override
    public String importProperty(List<Property> propertyList, boolean isUpdateSupport) {
        if (propertyList == null || propertyList.size() == 0) {
            throw new BaseException("导入资产数据不能为空");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (Property property : propertyList) {
            // 通过分类名补充分类id和分类ids
            List<Integer> categoryIdList = new ArrayList<>();
            String[] categoryNames = property.getCategoryName().split("/");
            List<PropertyCategory> categoryList = propertyCategoryMapper.selectList(null);
            for (String categoryName : categoryNames) {
                for (PropertyCategory propertyCategory : categoryList) {
                    if (categoryName.equals(propertyCategory.getName())) {
                        categoryIdList.add(propertyCategory.getId());
                    }
                }
            }
            if (categoryIdList.size()>0){
                int categoryId = categoryIdList.remove(categoryIdList.size() - 1);
                property.setCategoryId(categoryId);
                if (categoryIdList.size()>0){
                    StringBuilder categoryIds = new StringBuilder();
                    for (int i = 0; i < categoryIdList.size(); i++) {
                        categoryIds.append(categoryIdList.get(i));
                        if (i != categoryIdList.size() - 1) {
                            categoryIds.append(",");
                        }
                    }
                    property.setCategoryIds(categoryIds.toString());
                }
            }
            // 导入资产信息
            try {
                // 验证是否存在这个资产信息
                Property p = baseMapper.selectOne(new QueryWrapper<Property>().eq("cowin_num", property.getCowinNum()));
                if (p == null) {
                    baseMapper.insert(property);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、资产编号 " + property.getCowinNum() + " 导入成功");
                } else if (isUpdateSupport) {
                    this.update(property, new QueryWrapper<Property>().eq("cowin_num", property.getCowinNum()));
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、资产编号 " + property.getCowinNum() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、资产编号 " + property.getCowinNum() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = failureNum + "、资产编号 " + property.getCowinNum() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0) {
            if (successNum > 0) {
                successMsg.insert(0, "导入成功共 " + successNum + " 条，数据如下：");
                failureMsg.insert(0, "<br/>导入失败共 " + failureNum + " 条数据格式不正确，错误如下：");
                return failureMsg.insert(0, successMsg).toString();
            }
            failureMsg.insert(0, "很抱歉，全部导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            return failureMsg.toString();
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");

        }
        return successMsg.toString();
    }
}
